<template>
  <WsmTable
    ref="table"
    load-url="/rbac-server/rbac/resource/page"
    :change-load="true"
    v-model="queryFormType"
  >
    <template #title>{{ t('rbac.resource.title') }}</template>
    <template #query="{ data }">
      <el-form-item :label="t('rbac.resource.resState')">
        <el-radio-group v-model="data.resState.value">
          <el-radio value="">{{ t('rbac.resource.all') }}</el-radio>
          <el-radio value="0">{{ t('rbac.resource.disable') }}</el-radio>
          <el-radio value="1">{{ t('rbac.resource.enable') }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('rbac.resource.defaultResource')">
        <el-radio-group v-model="data.defaultResource.value">
          <el-radio value="">{{ t('rbac.resource.all') }}</el-radio>
          <el-radio value="0">{{ t('rbac.resource.authorization') }}</el-radio>
          <el-radio value="1">{{ t('rbac.resource.public') }}</el-radio>
        </el-radio-group>
      </el-form-item>
    </template>
    <template #button="{ row }">
      <el-button type="primary" v-hasPermi="'add'" @click="addForm.show()">{{
        t('common.add')
      }}</el-button>
      <template v-if="row">
        <el-button v-hasPermi="'update'" @click="update(row.id)" type="primary">{{
          t('common.update')
        }}</el-button>
        <el-button v-hasPermi="'delete'" @click="del(row.id)" type="danger">{{
          t('common.delete')
        }}</el-button>
        <el-button v-hasPermi="'config'" @click="config(row.id)" type="success">{{
          t('rbac.resource.config')
        }}</el-button>
        <template v-if="!row.defaultResource">
          <el-button v-hasPermi="'authInfo'" @click="authInfo(row.id)" type="warning">{{
            t('rbac.resource.authInfo')
          }}</el-button>
        </template>
      </template>
    </template>
    <template #column>
      <el-table-column prop="resState" :label="t('rbac.resource.resState')" width="70">
        <template #default="scope">
          <template v-if="scope.row.resState">
            <el-tag effect="dark" type="success">{{ t('rbac.resource.enable') }}</el-tag>
          </template>
          <template v-else>
            <el-tag effect="dark" type="danger">{{ t('rbac.resource.disable') }}</el-tag>
          </template>
        </template>
      </el-table-column>
      <el-table-column
        prop="defaultResource"
        :label="t('rbac.resource.defaultResource')"
        width="90"
      >
        <template #default="scope">
          <template v-if="scope.row.defaultResource">
            <el-tag effect="light" type="primary">{{ t('rbac.resource.public') }}</el-tag>
          </template>
          <template v-else>
            <el-tag effect="light" type="danger">{{ t('rbac.resource.authorization') }}</el-tag>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="resName" :label="t('rbac.resource.resName')" width="180" />
      <el-table-column prop="note" :label="t('rbac.resource.note')" />
    </template>
  </WsmTable>
  <WsmForm
    ref="addForm"
    v-model="addData"
    submit-url="/rbac-server/rbac/resource/add"
    label-width="100px"
    :init-data="initData"
    :title="t('rbac.resource.addTitle')"
    @success="flushed"
  >
    <template #default="{ form }">
      <el-row>
        <el-col :span="24">
          <el-form-item prop="resName" :label="t('rbac.resource.resName')">
            <el-input v-model="form.resName" :placeholder="t('rbac.resource.resName')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item prop="resState" :label="t('rbac.resource.resState')">
            <el-radio-group v-model="form.resState">
              <el-radio value="0">{{ t('rbac.resource.disable') }}</el-radio>
              <el-radio value="1">{{ t('rbac.resource.enable') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="defaultResource" :label="t('rbac.resource.defaultResource')">
            <el-radio-group v-model="form.defaultResource">
              <el-radio value="0">{{ t('rbac.resource.authorization') }}</el-radio>
              <el-radio value="1">{{ t('rbac.resource.public') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item prop="note" :label="t('rbac.resource.note')">
            <el-input type="textarea" v-model="form.note" :placeholder="t('rbac.resource.note')" />
          </el-form-item>
        </el-col>
      </el-row>
    </template>
  </WsmForm>

  <WsmForm
    ref="updateForm"
    v-model="updateData"
    load-url="/rbac-server/rbac/resource/get"
    label-width="100px"
    submit-url="/rbac-server/rbac/resource/update"
    :title="t('rbac.resource.updateTitle')"
    @success="flushed"
  >
    <template #default="{ form }">
      <el-row>
        <el-col :span="24">
          <el-form-item prop="resName" :label="t('rbac.resource.resName')">
            <el-input v-model="form.resName" :placeholder="t('rbac.resource.resName')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item prop="resState" :label="t('rbac.resource.resState')">
            <el-radio-group v-model="form.resState">
              <el-radio :value="false">{{ t('rbac.resource.disable') }}</el-radio>
              <el-radio :value="true">{{ t('rbac.resource.enable') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="defaultResource" :label="t('rbac.resource.defaultResource')">
            <el-radio-group v-model="form.defaultResource">
              <el-radio :value="false">{{ t('rbac.resource.authorization') }}</el-radio>
              <el-radio :value="true">{{ t('rbac.resource.public') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item prop="note" :label="t('rbac.resource.note')">
            <el-input type="textarea" v-model="form.note" :placeholder="t('rbac.resource.note')" />
          </el-form-item>
        </el-col>
      </el-row>
    </template>
  </WsmForm>

  <AuthorizeForm ref="configForm" />
  <AuthInfo ref="authInfoComponent" />
</template>
<script setup lang="ts">
import { WsmTable } from '@/components/WsmTable'
import { WsmForm } from '@/components/WsmForm'
import { QueryFormType, QueryType } from '@/components/WsmTable/src/types'
import { delApi } from '@/api/rbac/resource'
import { ref } from 'vue'
import { ElMessageBox } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { FormDataType } from '@/components/WsmForm/src/types'

import AuthorizeForm from './components/AuthorizeForm/AuthorizeForm.vue'
import AuthInfo from './components/AuthInfo.vue'

const { t } = useI18n()

//表格对象
const table = ref()

//新增表单对象
const addForm = ref()
const initData = ref({
  defaultResource: '0',
  resState: '1'
})
const addData: FormDataType = {
  resName: '',
  defaultResource: '0',
  resState: '1',
  note: ''
}

//修改表单对象
const updateForm = ref()
const updateData: FormDataType = {
  id: '',
  resName: '',
  defaultResource: '',
  resState: '',
  note: ''
}

//配置表单对象
const configForm = ref()

//查询定义
const queryFormType: QueryFormType = {
  resState: {
    value: '',
    queryType: QueryType.EQ
  },
  defaultResource: {
    value: '',
    queryType: QueryType.EQ
  }
}

const update = (id: string) => {
  updateForm.value.show({ id })
}
const del = async (id: string) => {
  ElMessageBox.confirm(t('common.delMessage'), t('common.delWarning'), {
    confirmButtonText: t('common.ok'),
    cancelButtonText: t('common.cancel'),
    type: 'warning'
  }).then(async () => {
    const rs = await delApi({ id })
    if (rs.success) {
      table.value.load(1)
    }
  })
}

const config = async (resId: string) => {
  configForm.value.show(resId)
}

const flushed = () => {
  table.value.load(1)
}

const authInfoComponent = ref()

const authInfo = (resId) => {
  authInfoComponent.value.show(resId)
}
</script>

<style scoped></style>
